package com.platform.config;

import com.platform.config.jackson.XlingJacksonMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * springboot2.0之后配置拦截器extends 的WebMvcConfigurerAdapter过时，取而代之的是WebMvcConfigurationSupport
 *
 * 查看源码发现: WebMvcConfigurerAdapter只是对WebMvcCofigurer的空实现,而WebMvcConfigurationSupport的实现的方法更全面
 *
 * 但是继承WebMvcConfigurationSupport会发现Spring Boot的WebMvc自动配置失效(WebMvcAutoConfiguration自动化配置)，导致无法视图解析器无法解析并返回到对应的视图
 * ————————————————
 * 版权声明：本文为CSDN博主「kehonghg」的原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接及本声明。
 * 原文链接：https://blog.csdn.net/kehonghg/article/details/81180731
 *
 */

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
    }


    /**
     *
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //在json转换之前先进行string转换
        converters.add(new StringHttpMessageConverter());
        //添加json转换
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jackson2HttpMessageConverter.setObjectMapper(new XlingJacksonMapper());
        converters.add(jackson2HttpMessageConverter);
        //5、追加默认转换器
        super.addDefaultHttpMessageConverters(converters);
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // web
        registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("login.html").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("index.html").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("main.html").addResourceLocations("classpath:/templates/");
        // swagger
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/statics/");
        registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/statics/swagger-ui/");

        // activiti
        registry.addResourceHandler("modeler.html").addResourceLocations("classpath:/statics/");
        registry.addResourceHandler("/diagram-viewer/**").addResourceLocations("classpath:/statics/diagram-viewer/");
        registry.addResourceHandler("/editor-app/**").addResourceLocations("classpath:/statics/editor-app/");
    }
}
